package com.tech.distribute_id.redis;

import com.tech.distribute_id.AbstractDistributeIdStrategy;
import com.tech.distribute_id.StrategyTypeEnum;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * @author litianlin
 * @date 2023/11/23 18:21
 * @description: redis方案，incrby
 */
@Component
public class RedisDistributeIdStrategy extends AbstractDistributeIdStrategy {
    public static final String ID_GENERATOR = "ID_GENERATOR";
    @Autowired
    private RedissonClient redisson;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public StrategyTypeEnum getStrategyType() {
        return StrategyTypeEnum.REIDS;
    }

    @Override
    public String getDistributeId() throws Exception {
        // 非单调递增
        return redisson.getIdGenerator(ID_GENERATOR).nextId()+"";
    }

    public String getDistributeId2() throws Exception {

        return redisTemplate.opsForValue().increment(ID_GENERATOR, 3L)+"";
    }
}
